<?php
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

include('/var/www/html/new/d/module/api/function.class.php');
$PFT_infunction = new AllFunction();

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

// $txt ='method=noticeOrderConsumed&requestParam=%7B%22data%22%3A%22PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHFtOnJlcXVlc3QgeG1sbnM6%5CncW09Imh0dHA6Ly90b3VyLmVjdHJpcC5jb20vMjAxNC9RTVJlcXVlc3REYXRhU2NoZW1hIiB4c2k6%5Cnc2NoZW1hTG9jYXRpb249Imh0dHA6Ly90b3VyLmVjdHJpcC5jb20vMjAxNC9RTVJlc3BvbnNlU2No%5CnZW1hIFFNUmVxdWVzdERhdGFTY2hlbWEtMS4xLjAueHNkIiB4bWxuczp4c2k9Imh0dHA6Ly93d3cu%5CndzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIj48cW06aGVhZGVyPjxxbTphcHBsaWNhdGlv%5Cnbj50b3VyLmVjdHJpcC5jb208L3FtOmFwcGxpY2F0aW9uPjxxbTpwcm9jZXNzb3I%2BRGF0YUV4Y2hh%5CnbmdlUHJvY2Vzc29yPC9xbTpwcm9jZXNzb3I%2BPHFtOnZlcnNpb24%2BMS42LjA8L3FtOnZlcnNpb24%2B%5CnPHFtOmJvZHlUeXBlPk5vdGljZU9yZGVyQ29uc3VtZWRSZXF1ZXN0Qm9keTwvcW06Ym9keVR5cGU%2B%5CnPHFtOmNyZWF0ZVVzZXI%2BY3l0dG9uZ2NoZW5nPC9xbTpjcmVhdGVVc2VyPjxxbTpjcmVhdGVUaW1l%5CnPjIwMTUtMDktMTAgMTU6MTI6MDA8L3FtOmNyZWF0ZVRpbWU%2BPHFtOnN1cHBsaWVySWRlbnRpdHk%2B%5CnQ1lUU1BXRFNCMkM8L3FtOnN1cHBsaWVySWRlbnRpdHk%2BPC9xbTpoZWFkZXI%2BPHFtOmJvZHkgeHNp%5CnOnR5cGU9InFtOk5vdGljZU9yZGVyQ29uc3VtZWRSZXF1ZXN0Qm9keSI%2BPHFtOm9yZGVySW5mbz48%5CncW06cGFydG5lcm9yZGVySWQ%2BMjAxNTA5MTA5OTkwMDAwMTk8L3FtOnBhcnRuZXJvcmRlcklkPjxx%5CnbTpvcmRlclF1YW50aXR5PjE8L3FtOm9yZGVyUXVhbnRpdHk%2BPHFtOnVzZVF1YW50aXR5PjE8L3Ft%5CnOnVzZVF1YW50aXR5PjxxbTpjb25zdW1lSW5mbz7mtojotLnkv6Hmga88L3FtOmNvbnN1bWVJbmZv%5CnPjwvcW06b3JkZXJJbmZvPjwvcW06Ym9keT48L3FtOnJlcXVlc3Q%2B%5Cn%22%2C%22signed%22%3A%227F03050B9E00E531D0B491BF90D41F7B%22%2C%22securityType%22%3A%22MD5%22%7D';

$txt=file_get_contents('php://input');
logfile($txt,'noticeOrderConsumed');

$arr = array();
parse_str($txt,$arr);
$rt=$arr['requestParam'];
$c = turn_xml($rt);

$cyt_orderid = (string)$c->orderInfo->partnerorderId; //畅游通的订单号
$cyt_orderQuantity = (int)$c->orderInfo->orderQuantity; //原始订单总票数
$cyt_useQuantity = (int)$c->orderInfo->useQuantity; //累计的消费张数	
if(!$cyt_orderid) exit("101");
$selorder = "select pftOrder from all_api_order where apiOrder='$cyt_orderid' limit 1";
$GLOBALS['le']->query($selorder);
if(!$GLOBALS['le']->fetch_assoc())	exit("101|0|0|0|0|0");		
$pftOrder = $GLOBALS['le']->f('pftOrder');  //查询订单号
$pft_order_xml = $PFT_infunction->Select_PFTorder($pftOrder);	
$UUlid = (int)$pft_order_xml->Rec->UUlid;
if($UUlid ==8510){
	$createUser = 'cytbaihe';
	$supplierIdentity = 'CYTSPQHDZOOB2C';
	$signkey = 'ae15qa77ed61a72ed';
}
elseif($UUlid ==8529){
	$createUser = 'cytheyuan';
	$supplierIdentity = 'CYTSPQHDZOOB2C';
	$signkey = 'cd50ac7r2ce9071c2';	
}
elseif($UUlid == 8997){
	$createUser = 'cytheyuan';
	$supplierIdentity = 'CYTSPBILUOTAB2C';
	$signkey = '9t8a6c12fa7yu52f3';
}
elseif($UUlid == 9290){
	$createUser = 'cyttongcheng';
	$supplierIdentity = 'CYTSPWDSB2C';
	$signkey = 'qwer';
}elseif($UUlid == 9412){//百合旅行社-碧螺塔
	$createUser = 'cytbaihe';
	$supplierIdentity = 'CYTSPBILUOTAB2C';
	$signkey = 's41c4def9ff25c62d';
}elseif($UUlid == 9389){//海源-集发
	$createUser = 'cytheyuan';
	$supplierIdentity = 'CYTSPJFGGY';
	$signkey = '9bcx8e9v6ca52beh9';
}elseif($UUlid == 10249){//百合-集发
	$createUser = 'cytbaihe';
	$supplierIdentity = 'CYTSPJFGGY';
	$signkey = '7wue4f2c74e5i8ed3';
}

if($arr['method']== 'noticeOrderConsumed'){ //用户消费通知
	$UUordernum = (string)$pft_order_xml->Rec->UUordernum;
	$UUsalerid = (string)$pft_order_xml->Rec->UUsalerid;	
	$UUcode = (string)$pft_order_xml->Rec->UUcode;		
	$chkIns = 499;
	if((string)$pft_order_xml->Rec->UUstatus==2) $chkIns = 498;
	$r = $PFT_infunction->Unified_Check_IN($UUsalerid,$UUcode,"",$chkIns);			
	if($r == 100){
		logfile($UUordernum."|".$r,"notice_success");
		$selorder = "update all_api_order  set oStatus=1 where apiOrder='$cyt_orderid' limit 1";
		$GLOBALS['le']->query($selorder);
		$repCode = 1000;
		$repMsg = '成功';
	}else{
		logfile($UUordernum."|".$r,"notice_fail");
		$repCode = 90000;
		$repMsg = "订单状态出错";
	}
	$time = date('Y-m-d H:i:s');	
	$Response_xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
	<response xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoResponseSchema"
		xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
		<header>
			<application>tour.ectrip.com</application>
			<processor>DataExchangeProcessor</processor>
			<version>v2.0.0</version>
			<bodyType>NoticeOrderConsumedResponseBody</bodyType>
			<createUser>{$createUser}</createUser>
			<createTime>{$time}</createTime>
			<code>{$repCode}</code>
			<describe>{$repMsg}</describe>
		</header>
		<body xsi:type="NoticeOrderConsumedResponseBody">
			<message></message>
		</body>
	</response>
XML;
		$cyt_Response = signed($Response_xml,$signkey);
		echo $cyt_Response;
		
}
elseif($arr['method']== 'noticeOrderPrintSuccess'){ //出票通知,不执行任何操作
	$time = date('Y-m-d H:i:s');
	$Response_xml=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoResponseSchema"
xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
	<application>tour.ectrip.com</application>
	<processor>DataExchangeProcessor</processor>
	<version>v2.0.0</version>
	<bodyType>NoticeOrderPrintSuccessResponseBody</bodyType>
	<createUser>$createUser</createUser>
	<createTime>$time</createTime>
	<code>1000</code>
	<describe>成功接收</describe>
</header>
<body xsi:type="NoticeOrderPrintSuccessResponseBody">
	<message></message>
</body>
</response>
XML;
	$cyt_Response = signed($Response_xml,$signkey);
	echo $cyt_Response;	
}
elseif($arr['method']== 'noticeOrderRefundApproveResult'){//通知OTA申请退款结果
	$time = date('Y-m-d H:i:s');
	$Response_xml=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoResponseSchema"
	xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>tour.ectrip.com</application>
		<processor>DataExchangeProcessor</processor>
		<version>v2.0.0</version>
		<bodyType>NoticeOrderRefundApproveResultResponseBody</bodyType>
		<createUser>$createUser</createUser>
		<createTime>$time</createTime>
		<code>1000</code>
		<describe>成功</describe>
	</header>
	<body xsi:type="NoticeOrderRefundApproveResultResponseBody">
		<message></message>
	</body>
</response>
XML;
	$cyt_Response = signed($Response_xml,$signkey);
	echo $cyt_Response;	
}	


function turn_xml($rt){
	$arr1=json_decode($rt);
	$xml=base64_decode($arr1->data);
	$cyt_Response_data = str_replace("qm:","",$xml);
	$c=simpleXML_load_string($cyt_Response_data);
	return $c->body; 
}

function signed($request_xml,$signkey){ //这里是加密
		$bstr=base64_encode($request_xml);
		$signed=strtoupper(md5($signkey.$bstr));
		$arr=array('data'=>$bstr,'signed'=>$signed,'securityType'=>'MD5');
		
		return json_encode($arr);
}

function Err_Word($r){
	$errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);
	$eword=$errCode[$r];
	if (!$eword) $eword='其他错误';
	return $eword;
}

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ChangYouTong/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ChangYouTong/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ChangYouTong/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}

?>